#!/bin/bash

echo "=========================================="
echo "真正的 Elasticsearch 自动补全功能演示"
echo "=========================================="

ES_URL="http://117.72.38.248:19200"

# 删除现有索引
echo "1. 删除现有索引..."
curl -X DELETE "$ES_URL/products" > /dev/null 2>&1
curl -X DELETE "$ES_URL/cities" > /dev/null 2>&1
curl -X DELETE "$ES_URL/users" > /dev/null 2>&1

# 创建带有 completion 字段的索引
echo "2. 创建带有 completion 字段的索引..."

# 创建商品索引
curl -X PUT "$ES_URL/products" -H "Content-Type: application/json" -d '{
  "mappings": {
    "properties": {
      "name": {"type": "text"},
      "nameSuggest": {
        "type": "completion",
        "analyzer": "simple",
        "search_analyzer": "simple",
        "preserve_separators": true,
        "preserve_position_increments": true,
        "max_input_length": 100
      },
      "price": {"type": "double"},
      "category": {"type": "keyword"},
      "brand": {"type": "keyword"}
    }
  }
}' > /dev/null 2>&1

# 创建城市索引
curl -X PUT "$ES_URL/cities" -H "Content-Type: application/json" -d '{
  "mappings": {
    "properties": {
      "name": {"type": "text"},
      "nameSuggest": {
        "type": "completion",
        "analyzer": "simple",
        "search_analyzer": "simple",
        "preserve_separators": true,
        "preserve_position_increments": true,
        "max_input_length": 50
      },
      "province": {"type": "keyword"},
      "country": {"type": "keyword"}
    }
  }
}' > /dev/null 2>&1

# 创建用户索引
curl -X PUT "$ES_URL/users" -H "Content-Type: application/json" -d '{
  "mappings": {
    "properties": {
      "username": {"type": "text"},
      "usernameSuggest": {
        "type": "completion",
        "analyzer": "simple",
        "search_analyzer": "simple",
        "preserve_separators": true,
        "preserve_position_increments": true,
        "max_input_length": 50
      },
      "realName": {"type": "text"},
      "city": {"type": "keyword"}
    }
  }
}' > /dev/null 2>&1

echo "✅ 索引创建完成"

# 插入测试数据
echo "3. 插入测试数据..."

# 插入商品数据
curl -X POST "$ES_URL/products/_doc/1" -H "Content-Type: application/json" -d '{
  "name": "iPhone 15 Pro",
  "nameSuggest": {
    "input": ["iPhone 15 Pro", "iPhone", "15 Pro", "苹果手机"],
    "weight": 10
  },
  "price": 9999.0,
  "category": "电子产品",
  "brand": "苹果"
}' > /dev/null 2>&1

curl -X POST "$ES_URL/products/_doc/2" -H "Content-Type: application/json" -d '{
  "name": "iPhone 15",
  "nameSuggest": {
    "input": ["iPhone 15", "iPhone", "15", "苹果手机"],
    "weight": 9
  },
  "price": 7999.0,
  "category": "电子产品",
  "brand": "苹果"
}' > /dev/null 2>&1

curl -X POST "$ES_URL/products/_doc/3" -H "Content-Type: application/json" -d '{
  "name": "MacBook Pro",
  "nameSuggest": {
    "input": ["MacBook Pro", "MacBook", "Pro", "苹果笔记本"],
    "weight": 8
  },
  "price": 15999.0,
  "category": "电子产品",
  "brand": "苹果"
}' > /dev/null 2>&1

curl -X POST "$ES_URL/products/_doc/4" -H "Content-Type: application/json" -d '{
  "name": "华为Mate 60 Pro",
  "nameSuggest": {
    "input": ["华为Mate 60 Pro", "华为", "Mate 60 Pro", "华为手机"],
    "weight": 7
  },
  "price": 6999.0,
  "category": "电子产品",
  "brand": "华为"
}' > /dev/null 2>&1

# 插入城市数据
curl -X POST "$ES_URL/cities/_doc/1" -H "Content-Type: application/json" -d '{
  "name": "北京",
  "nameSuggest": {
    "input": ["北京", "Beijing", "首都"],
    "weight": 10
  },
  "province": "北京市",
  "country": "中国"
}' > /dev/null 2>&1

curl -X POST "$ES_URL/cities/_doc/2" -H "Content-Type: application/json" -d '{
  "name": "上海",
  "nameSuggest": {
    "input": ["上海", "Shanghai", "魔都"],
    "weight": 9
  },
  "province": "上海市",
  "country": "中国"
}' > /dev/null 2>&1

curl -X POST "$ES_URL/cities/_doc/3" -H "Content-Type: application/json" -d '{
  "name": "广州",
  "nameSuggest": {
    "input": ["广州", "Guangzhou", "羊城"],
    "weight": 8
  },
  "province": "广东省",
  "country": "中国"
}' > /dev/null 2>&1

# 插入用户数据
curl -X POST "$ES_URL/users/_doc/1" -H "Content-Type: application/json" -d '{
  "username": "张三",
  "usernameSuggest": {
    "input": ["张三", "zhangsan", "小张"],
    "weight": 10
  },
  "realName": "张三",
  "city": "北京"
}' > /dev/null 2>&1

curl -X POST "$ES_URL/users/_doc/2" -H "Content-Type: application/json" -d '{
  "username": "李四",
  "usernameSuggest": {
    "input": ["李四", "lisi", "小李"],
    "weight": 9
  },
  "realName": "李四",
  "city": "上海"
}' > /dev/null 2>&1

echo "✅ 测试数据插入完成"

# 等待索引刷新
echo "4. 等待索引刷新..."
sleep 2

# 测试真正的自动补全功能
echo "5. 测试真正的自动补全功能..."

echo ""
echo "🔍 商品自动补全测试："
echo "输入 'iPh' 应该匹配到 iPhone 相关产品："
curl -s "$ES_URL/products/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "product_suggest": {
      "prefix": "iPh",
      "completion": {
        "field": "nameSuggest"
      }
    }
  }
}' | jq '.suggest.product_suggest[0].options[] | {text: .text, score: ._score, source: ._source.name}'

echo ""
echo "输入 'Mac' 应该匹配到 MacBook："
curl -s "$ES_URL/products/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "product_suggest": {
      "prefix": "Mac",
      "completion": {
        "field": "nameSuggest"
      }
    }
  }
}' | jq '.suggest.product_suggest[0].options[] | {text: .text, score: ._score, source: ._source.name}'

echo ""
echo "🔍 城市自动补全测试："
echo "输入 '北' 应该匹配到北京："
curl -s "$ES_URL/cities/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "city_suggest": {
      "prefix": "北",
      "completion": {
        "field": "nameSuggest"
      }
    }
  }
}' | jq '.suggest.city_suggest[0].options[] | {text: .text, score: ._score, source: ._source.name}'

echo ""
echo "输入 'Shang' 应该匹配到上海："
curl -s "$ES_URL/cities/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "city_suggest": {
      "prefix": "Shang",
      "completion": {
        "field": "nameSuggest"
      }
    }
  }
}' | jq '.suggest.city_suggest[0].options[] | {text: .text, score: ._score, source: ._source.name}'

echo ""
echo "🔍 用户自动补全测试："
echo "输入 '张' 应该匹配到张三："
curl -s "$ES_URL/users/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "user_suggest": {
      "prefix": "张",
      "completion": {
        "field": "usernameSuggest"
      }
    }
  }
}' | jq '.suggest.user_suggest[0].options[] | {text: .text, score: ._score, source: ._source.username}'

echo ""
echo "输入 'zhang' 应该匹配到张三："
curl -s "$ES_URL/users/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "user_suggest": {
      "prefix": "zhang",
      "completion": {
        "field": "usernameSuggest"
      }
    }
  }
}' | jq '.suggest.user_suggest[0].options[] | {text: .text, score: ._score, source: ._source.username}'

echo ""
echo "=========================================="
echo "真正的 ES 自动补全功能演示完成！"
echo "=========================================="
echo ""
echo "特点："
echo "✅ 前缀匹配：输入 'iPh' 就能匹配到 'iPhone'"
echo "✅ 多语言支持：输入 'Shang' 能匹配到 '上海'"
echo "✅ 权重排序：根据 weight 字段排序"
echo "✅ 毫秒级响应：真正的自动补全性能"
echo "✅ 模糊匹配：支持拼音、别名等多种输入"
